import { EnumProtocolType } from "../../../../common/protocols/EnumProtocolType";
import ConstProtocolRouterMap from "../../../../common/protocols/ProtocolRouterMap";
import { IMsg } from "../../../../framework/service/Components/WorkOrderInfo";
import { LogicCenter } from "../controller/LogicCenter";
import { RobotEntity } from "./RobotEntity";

export class RobotConnection {



    private _callBacks: { [key: number]: (msg: IMsg) => void } = {}

    private _user: RobotEntity;


    //**这个是模拟接收来自server的消息 */
    public sendUTF(msgStr: string): void {
        const msg: IMsg = JSON.parse(msgStr);


        if (this._callBacks[msg.cmdid]) {
            this._callBacks[msg.cmdid](msg);
            delete this._callBacks[msg.cmdid];

        }
        else {
            this._user.onReceiveMsg(msg);
        }



    }


    public sendMsg(msg: IMsg, callback: (sc: IMsg) => void): void {


        const scMsgId: EnumProtocolType = ConstProtocolRouterMap[msg.cmdid];
        //这里是模拟将消息发送给服务器

        this._callBacks[msg.cmdid + 10000] = callback;
        LogicCenter.inst.receiveTCPMessage(msg, this);

    }


    public setRobot(robot: RobotEntity): void {
        this._user = robot;
    }

    //**断开与服务器的连接 */
    public close():void{
        
    }
}